package com.larry.mr.table;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @ProjectName map-reduce
 * @Author larry
 * @Date 10:20 2022/4/8
 * @Description TODO
 **/
public class TableMapper extends Mapper<LongWritable, Text,Text,TableBean> {

    String name;
    TableBean tableBean = new TableBean();
    Text k = new Text();

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {

        FileSplit inputSplit = (FileSplit) context.getInputSplit();
        name = inputSplit.getPath().getName();

    }

    /**
     * id	pid	amount
     * 1001	01	1
     *
     * pid	pname
     * 01	小米
     * @param key
     * @param value
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {


        String[] fields = value.toString().split("\t");

        if (name.startsWith("order")){
            tableBean.setId(fields[0]);
            tableBean.setPid(fields[1]);
            tableBean.setAmount(Integer.parseInt(fields[2]));
            tableBean.setPname("");
            tableBean.setFlag("order");

            k.set(fields[1]);
        }else {
            tableBean.setId("");
            tableBean.setPid(fields[0]);
            tableBean.setPname(fields[1]);
            tableBean.setAmount(0);
            tableBean.setFlag("pd");

            k.set(fields[0]);
        }

        context.write(k,tableBean);

    }
}
